﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;

namespace MulticastTV
{
    /// <summary>
    /// Export internal (channel informations) xml to client and vlc rpc server playlists.
    /// </summary>

    class ExportToVLM
    {
        public void Vlm(string file)
        {
            // example:
            //    new slo1 vod enabled
            //    setup slo1 input "udp://@239.255.0.1:5002" 
            
            StreamWriter sw = File.CreateText(file);
            int prevchan = 0;
            DataView dw = ChannelTable.menu.Tables["Menu"].DefaultView;
            dw.Sort = "Chan";
            foreach (DataRowView dr in dw)
            {
                string ip = dr[0].ToString();
                int ch = (int)dr[1];
                string name = dr[2].ToString();
                string audio = dr[4].ToString();
                if (prevchan != ch && ch != 0)
                {
                    sw.WriteLine("new " + name.Replace(" ","") + " vod enabled ");
                    sw.WriteLine("setup " + name.Replace(" ", "") + " input udp://@" + ip);
                }
                else
                {
                    sw.WriteLine("new " + name.Replace(" ", "") + "(" + audio.Replace(" ", "") + ") vod enabled ");
                    sw.WriteLine("setup " + name.Replace(" ", "") + "(" + audio.Replace(" ", "") + ") input udp://@" + ip);
                }
                prevchan = ch;
            }
            sw.Close();
        }

        public void M3u(string file, string rtpip)
        { 
            //    #EXTM3U
            //    #EXTINF:0,1 - Slo1
            //    rtsp://192.168.0.2:5554/slo1 

            StreamWriter sw = File.CreateText(file);
            sw.WriteLine("#EXTM3U");
            int prevchan = 0;
            DataView dw = ChannelTable.menu.Tables["Menu"].DefaultView;
            dw.Sort = "Chan";
            foreach (DataRowView dr in dw)
            {
                string ip = dr[0].ToString();
                int ch = (int)dr[1];
                string name = dr[2].ToString();
                string audio = dr[4].ToString();
                if (prevchan != ch && ch != 0)
                {
                    sw.WriteLine("#EXTINF:-1," + ch + " - " + name);
                    sw.WriteLine("rtsp://" + rtpip + "/" + name.Replace(" ", ""));
                }
                else
                {
                    sw.WriteLine("#EXTINF:-1," + ch + " - " + name + " (" + audio + ")");
                    sw.WriteLine("rtsp://" + rtpip + "/" + name.Replace(" ", "") + "(" + audio + ")");
                }
                prevchan = ch;
            }
            sw.Close();
        }
    }
}
